﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Plyaer : MonoBehaviour
{
    public LayerMask backgroundLay;
    public float speed = 5;
    public float jump = 5;
    public Transform Hp;

    int jumpCount = 0;
    int hp = 1;

    Renderer rd;
    Animator am;
    Transform addHp;
    bool isKey;
    void Start()
    {
        rd = GetComponent<Renderer>();

        am = GetComponent<Animator>();

        foreach (var item in am.runtimeAnimatorController.animationClips)
        {
            if (hp > 1)
            {
                if (item.name.Equals("PlayerDie"))
                {
                    AnimationEvent ae = new AnimationEvent();
                    ae.time = item.length;
                    ae.functionName = "PlayerDie";
                }
            }
        }
    }

    //void Update()
    //{
    //    // 判断是否在相机的范围
    //    if (!IsVisibleFrom(rd,Camera.main))
    //    {
    //        hp = 0;
    //        Destroy(gameObject);
    //    }

    //    Move();
    //}

    // 血量大于一 死亡动画 当成受伤动画
    void PlayerDie()
    {
        am.SetBool("dei", false);
    }

    /// <summary>
    /// 触发
    /// </summary>
    /// <param name="other"></param>
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "NPC")
        {
            am.SetBool("die", true);
            Destroy(transform.parent.GetChild(hp--).gameObject);
            if (hp <= 0)
            {
                hp = 0;
                am.SetBool("die", true);
                am.SetBool("run", false);
                am.SetBool("jump", false);
                Destroy(gameObject, 0.5F);
            }
        }

        if (other.transform.tag == "Key")
        {
            isKey = true;
            Destroy(other.gameObject);
            Debug.Log("获取宝箱钥匙");
        }


        if (other.transform.tag == "Treasure")
        {
            if (isKey)
            {
                other.transform.GetComponent<Animator>().SetBool("open", true);

                if (hp < 2)
                {
                    addHp = Instantiate(Hp).transform;
                    addHp.parent = Hp.parent;
                    addHp.position = addHp.parent.GetChild(hp++).position + Vector3.right * 0.5F;
                }
                
                isKey = false;
            }
        }

    }


    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.transform.tag == "Slime")
        {
            am.SetBool("die", true);
            Destroy(col.gameObject, 1);
        }
    }

    void OnCollisionExit2D(Collision2D col)
    {
        am.SetBool("die", false);
    }


    void Move()
    {
        if (hp > 0)
        {
            if (Input.GetKey(KeyCode.A))
            {
                am.SetBool("run", true);
                transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x) * -1, transform.localScale.y, transform.localScale.z);
                transform.Translate(transform.right * -speed * Time.deltaTime);
            }
            if (Input.GetKeyUp(KeyCode.A))
            {
                am.SetBool("run", false);
            }

            if (Input.GetKey(KeyCode.D))
            {
                am.SetBool("run", true);
                transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
                transform.Translate(transform.right * speed * Time.deltaTime);
            }
            if (Input.GetKeyUp(KeyCode.D))
            {
                am.SetBool("run", false);
            }

            Debug.DrawRay(transform.position, Vector2.down);
            // 是否接触地面
            if (Physics2D.Raycast(transform.position, Vector2.down, 1, backgroundLay))
            {
                jumpCount = 0;
            }

            if (Input.GetKeyDown(KeyCode.Space))
            {
                jumpCount++;
                // 连跳检测
                if (jumpCount < 2)
                {
                    am.SetBool("jump", true);
                    gameObject.GetComponent<Rigidbody2D>().velocity = transform.up * jump;
                }
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                am.SetBool("jump", false);
            }
        }

    }

    // -- 判断物体是否在相机的6个面内
    //    bool IsVisibleFrom(Renderer renderer, Camera camera)
    //    {
    //        Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
    //        return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
    //    }
}
